/**
 * Created by Administrator on 2017/7/27.
 */
define(function(require, exports){
	var $ = require("jquery");
	var util = require('util');
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	/**
	 * 复制广告位代码
	 */
	var CampaignCode = view.container.extend({
		init: function( config, parent ) {
			config = pubjs.conf(config, {
				'class': 'P-campaignCode',
				'target': parent,
				'url': 'campaign/code',
				'downUrl': '/api/dsp/export/campaign/code',
				'tplFile': '/tpl/campaign/modules/campaignCode.html',
				'param': null,
				'view_model':{
					'lists': [
						{
							'mediaName':'',
							'ads':[]
						}
					]
				}
			});
			this.Super('init', arguments);
			this.$data = null;
			this.$ready = false;
		},
		afterBuild: function() {
			this.buildFromTemplate({
				'add':{
					'def': LANG('新建监控'),
					'href': '#serving/monitorEdit',
					'href_target': '_self'
				},
			});
			var el = this.getDOM();
			this.uiProxy(el, 'button.download', 'click', 'eventDownload');
			// this.uiProxy(el, '.copy a', 'mouseenter', 'eventCopy');
		},
		eventDownload: function(evt, elm){
			var c = this.getConfig();
			var id = this.$id;
			if(id){
				var url = pubjs.data.resolve(c.downUrl, {
					'isExport': 'export',
					'_id': +id
				});
				window.open(url);
			}
		},
		copyComplete: function(){
			pubjs.notify(LANG('复制成功'), '', 'success', 1000);
		},
		reset: function(){
			var table = this.$el.find('.table-code');
			table.html('');
			this.$data = null;
			return this;
		},
		setData: function(data){
			this.reset();
			this.$data = data;
			this.buildTable(data);
		},
		load: function(id){
			var c = this.getConfig();
			var param = {
				'_id': id
			};
			this.$id = id;
			this.showLoading();
			pubjs.mc.send(c.url, param, this.onData.bind(this));
		},
		onData: function(err, data){
			this.hideLoading();
			if(err){
				pubjs.alert(err.message);
				return false;
			}
			// 转换数据
			var list = {};
			util.each(data.items, function(item){
				item.AdType = data.campaign.AdType;
				if (!list[item.MassMediaName]){
					list[item.MassMediaName] = [item];
				}else {
					list[item.MassMediaName].push(item);
				}
			});
			var result = [];
			util.each(list, function(item, key){
				var tmp ={};
				tmp.mediaName = key;
				tmp.item = item;
				result.push(tmp);
			});
			this.setData(result);
		},
		buildTable: function(data){
			var self = this;
			self.$el = self.getDOM();
			var table = self.$el.find('.table-code');
			var tbody = $('<tbody></tbody>');
			data = data || self.$data;
			if(data && data.length){
				util.each(data, function(opt){
					var mediaName = $(['<tr><th colspan="4" >', opt.mediaName, '</th></tr>'].join(''));
					mediaName.appendTo(tbody);

					util.each(opt.item, function(item, index){
						var code;
						if(item.Miss){
							code = $([
								'<tr>',
								'<td class="id">', index + 1 ,'</td>',
								'<td class="name">', item.AdPositionName, '</td>',
								'<td class="miss">创意包“ <b>',item.SweetyName,'</b> ”缺少 <b>',item.WidthHeight,'</b> 的创意，请先<a href="#sweety/edit/',item.SweetyId,'" target="_blank">添加</a>，再获取代码</td>',
								'<td class="add">-</td>',
								'</tr>'
							].join(''));
						}else{
							var CreativeUrl = (item.CreativeUrl||'').replace('http://manager.biddingx.com/', 'http://stuff.cdn.biddingx.com/');
							if(item.AdType === 2){
								code = $([
									'<tr>',
									'<td class="id" rowspan="2">',index + 1,'</td>',
									'<td class="name" rowspan="2">',item.AdPositionName,'</td>',
									'<td class="url">点击跳转代码: <span>',item.Url,'</span></td>',
									'<td class="copy"><a href="javascript:void(0);">复制</a></td>',
									'</tr>',
									'<tr>',
									'<td class="url">素材地址: <span>',CreativeUrl,'</span></td>',
									'<td class="copy"><a href="javascript:void(0);">复制</a></td>',
									'</tr>'
								].join(''));

							}else{
								code = $([
									'<tr>',
									'<td class="id" rowspan="3">',index + 1,'</td>',
									'<td class="name" rowspan="3">',item.AdPositionName,'</td>',
									'<td class="url">曝光监测代码: <span>',item.MonitorUrl,'</span></td>',
									'<td class="copy"><a href="javascript:void(0);">复制</a></td>',
									'</tr>',
									'<tr>',
									'<td class="url">点击跳转代码: <span>', item.Url, '</span></td>',
									'<td class="copy"><a href="javascript:void(0);">复制</a></td>',
									'</tr>',
									'<tr>',
									'<td class="url">素材地址: <span>',CreativeUrl,'</span></td>',
									'<td class="copy"><a href="javascript:void(0);">复制</a></td>',
									'</tr>'
								].join(''));
							}
						}
						code.appendTo(tbody);
					});
				});
			}
			tbody.appendTo(table);

			// 加入复制事件
			var copys = table.find('.copy a');
			$.each(copys, function(idx, copy){
				var target = $(copy).parent().prev().find('span');
				pubjs.customClipboard(target, $(copy), self.copyComplete);
			});

		}
	});
	exports.base = CampaignCode;
});